package com.meixiang.activity.account;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.manager.AppointmentMessageActivity;
import com.meixiang.adapter.AccountMessageAdapter;
import com.meixiang.entity.account.BookingListMessage;
import com.meixiang.entity.account.OrderShipMessage;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 账户消息
 * author：wangzuzhen on 2016/5/6 0006 10:33.
 */
public class AccountMessageActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {
    @Bind(R.id.iv_picture)
    ImageView mIvPicture;
    @Bind(R.id.tv_msg_title)
    TextView mTvMsgTitle;
    @Bind(R.id.tv_msg_time)
    TextView mTvMsgTime;
    @Bind(R.id.tv_msg_content)
    TextView mTvMsgContent;
    @Bind(R.id.rl_msg)
    RelativeLayout mRlMsg;
    @Bind(R.id.iv_picture2)
    ImageView mIvPicture2;
    @Bind(R.id.tv_msg_title2)
    TextView mTvMsgTitle2;
    @Bind(R.id.tv_msg_time2)
    TextView mTvMsgTime2;
    @Bind(R.id.tv_msg_content2)
    TextView mTvMsgContent2;
    @Bind(R.id.rl_msg2)
    RelativeLayout mRlMsg2;
    @Bind(R.id.iv_picture3)
    ImageView mIvPicture3;
    @Bind(R.id.tv_msg_title3)
    TextView mTvMsgTitle3;
    @Bind(R.id.tv_msg_time3)
    TextView mTvMsgTime3;
    @Bind(R.id.tv_msg_content3)
    TextView mTvMsgContent3;
    @Bind(R.id.rl_msg3)
    RelativeLayout mRlMsg3;
    /*  @Bind(R.id.swipe_target)
        RecyclerView mSwipeTarget;
        @Bind(R.id.refresh)
        SwipeToLoadLayout mRefresh;*/
    private Activity mActivity;
    private AccountMessageAdapter mAdapter;
    BookingListMessage bookingListMessage;
    OrderShipMessage orderShipMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_account_message);
        ButterKnife.bind(this);
        mActivity = AccountMessageActivity.this;
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle("消息");
    }

    @Override
    protected void initView() {
//        mRefresh.setLoadMoreEnabled(false);
//        mRefresh.setRefreshEnabled(false);
//
//        mAdapter = new AccountMessageAdapter(context);
//        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(context);
//        mSwipeTarget.setLayoutManager(layoutManager);
//        mSwipeTarget.setAdapter(mAdapter);

//        mAdapter.setOnItemClickListener(new OnItemClick() {
//            @Override
//            public void onItemClick(View view, int position) {
//
//                if (position == 1) {
//                    startActivity(new Intent(context, LogisticsMessageActivity.class));//物流消息
//                }
//                if (position == 2) {
//                    startActivity(new Intent(context, AppointmentMessageActivity.class));//预约消息
//                }
//            }
//        });
    }

    @Override
    protected void setData() {
//        status.showLoading();
//        getMessageData();
    }

    private void getMessageData(/* final boolean isPull*/) {
        HttpParams params = new HttpParams();
        HttpUtils.post(Config.SERVER_URL + "sys/pushMessage/messageNum", params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
//                Tool.showTextToast(context, message);
//                if (isPull) {
//                    mAdapter.clearData();
//                }
//                mPageNo = result.optInt("pageNo");
//                totalPage = result.optInt("totalPage");
//                List<AccountMessage> mList = AbJsonUtil.fromJson(result.optJSONArray("list").toString(), new TypeToken<List<AccountMessage>>() {
//                });
//                if (mList != null && mList.size() > 0) {
//                    status.removeView();
//                    mAdapter.addAll(mList);
//                } else {
//                    status.showNoData("暂无数据");
//                }
                orderShipMessage = AbJsonUtil.fromJson(result.optJSONObject("orderShipMessage").toString(), OrderShipMessage.class);
                bookingListMessage = AbJsonUtil.fromJson(result.optJSONObject("bookingListMessage").toString(), BookingListMessage.class);
                if (orderShipMessage != null) {
                    mTvMsgTime2.setText(orderShipMessage.getCreateTime());
                    mTvMsgContent2.setText(orderShipMessage.getTicker());
                }
                if (bookingListMessage != null) {
                    mTvMsgTime3.setText(bookingListMessage.getCreateTime());
                    mTvMsgContent3.setText(bookingListMessage.getTicker());
                }
            }

            @Override
            public void onError(String code, String message) {
                Tool.showTextToast(context, message);
//                status.showNoNewWork(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        getMessageData(isPull);
//                    }
//                });
//
//                if (message.equals("暂无数据")) {
//                    status.showNoData("暂无数据");
//                } else {
//                    status.showNoData(message, "重新加载", new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            status.showLoading();
//                            getMessageData(isPull);
//                        }
//                    });
//                }
            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                super.onAfter(isFromCache, jsonObject, call, response, e);
//                if (mRefresh.isRefreshing()) {
//                    mRefresh.setRefreshing(false);
//                } else {
//                    mRefresh.setLoadingMore(false);
//                }
            }
        });
    }


    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
//        if (mPageNo<totalPage){
//            mPageNo++;
//            getMessageData(false);
//        }else{
//            AbToastUtil.showToast(activity, "已无更多数据");
//            mRefresh.setLoadingMore(false);
//        }
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
//        mPageNo = 1;
//        getMessageData(true);
    }


    @Override
    protected void onResume() {
        super.onResume();
        getMessageData();
    }

    @OnClick({R.id.rl_msg, R.id.rl_msg2, R.id.rl_msg3})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.rl_msg:

                break;
            case R.id.rl_msg2:
                //物流消息
                intent.setClass(context, LogisticsMessageActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_msg3:
                //预约消息
                intent.setClass(context, AppointmentMessageActivity.class);
                startActivity(intent);
                break;
        }
    }
}
